package org.example.printexercise;

import java.util.ArrayList;
import java.util.List;

public class TextComparator {

    public static CompareResult compareText(String expected, String actual) {
        if (expected == null) expected = "";
        if (actual == null) actual = "";

        List<String> expectedWords = splitIntoWords(expected);
        List<String> actualWords = splitIntoWords(actual);

        // 简单的逐词比较（简化版）
        StringBuilder result = new StringBuilder();
        int errorCount = 0;
        int minLength = Math.min(expectedWords.size(), actualWords.size());

        for (int i = 0; i < minLength; i++) {
            if (expectedWords.get(i).equals(actualWords.get(i))) {
                result.append(actualWords.get(i));
            } else {
                result.append("[RED]").append(actualWords.get(i)).append("[/RED]");
                errorCount++;
            }

            // 添加空格（如果不是最后一个词）
            if (i < minLength - 1) {
                result.append(" ");
            }
        }

        // 处理多余的部分
        if (actualWords.size() > expectedWords.size()) {
            if (minLength > 0) result.append(" ");
            for (int i = minLength; i < actualWords.size(); i++) {
                result.append("[RED]").append(actualWords.get(i)).append("[/RED]");
                errorCount++;
                if (i < actualWords.size() - 1) {
                    result.append(" ");
                }
            }
        }

        // 处理缺少的部分（不计入错误，因为不显示）
        if (expectedWords.size() > actualWords.size()) {
            errorCount += (expectedWords.size() - actualWords.size());
        }

        return new CompareResult(result.toString(), errorCount);
    }

    private static List<String> splitIntoWords(String text) {
        List<String> words = new ArrayList<>();
        if (text == null || text.isEmpty()) {
            return words;
        }

        StringBuilder currentWord = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (Character.isWhitespace(c)) {
                if (currentWord.length() > 0) {
                    words.add(currentWord.toString());
                    currentWord.setLength(0);
                }
            } else if (isPunctuation(c)) {
                if (currentWord.length() > 0) {
                    words.add(currentWord.toString());
                    currentWord.setLength(0);
                }
                words.add(String.valueOf(c));
            } else {
                currentWord.append(c);
            }
        }

        if (currentWord.length() > 0) {
            words.add(currentWord.toString());
        }

        return words;
    }

    private static boolean isPunctuation(char c) {
        return ",.;!?，。！？".indexOf(c) != -1;
    }

    public static class CompareResult {
        private String coloredText;
        private int errorCount;

        public CompareResult(String coloredText, int errorCount) {
            this.coloredText = coloredText;
            this.errorCount = errorCount;
        }

        public String getColoredText() { return coloredText; }
        public int getErrorCount() { return errorCount; }
    }
}